package cn.qrx.gateway.session;

import cn.qrx.gateway.session.handlers.SessionServerHandler;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpRequestDecoder;
import io.netty.handler.codec.http.HttpResponseEncoder;

/**
 * @author 屈燃希
 * @date 2025/10/1 14:44
 * @content 会话通道初始化器，用于配置Netty通道的管道
 */
public class SessionChannelInitializer extends ChannelInitializer<SocketChannel> {

    /**
     * 初始化SocketChannel的管道，添加必要的处理器
     * @param channel 要初始化的SocketChannel
     * @throws Exception 可能抛出的异常
     */
    @Override
    protected void initChannel(SocketChannel channel) throws Exception {
        // 获取通道的管道
        ChannelPipeline line = channel.pipeline();
        // 添加HTTP请求解码器，用于解码HTTP请求
        line.addLast(new HttpRequestDecoder());
        // 添加HTTP响应编码器，用于编码HTTP响应
        line.addLast(new HttpResponseEncoder());
        // 添加HTTP对象聚合器，聚合HTTP消息为FullHttpRequest/FullHttpResponse
        // 设置聚合的最大内容长度为1MB
        line.addLast(new HttpObjectAggregator(1024 * 1024));
        // 添加会话服务器处理器，处理实际的会话逻辑
        line.addLast(new SessionServerHandler());
    }

}
